
--选择英雄
local empty = {}
class.herospSelect = extends(class.button) {
    reload = true,

    new = function (parent,x,y,w,h)
        local panel = class.herospSelect:builder 
        {
            parent = parent,
            x = x,
            y = y,
            w = w,
            h = h,
        }
        return panel
    end,
    init = function (self)
        class.button.init(self)
        self:set_normal_image([[textures\black32.blp]])
        self.highlight = self:add_panel([[UI\选中框.tga]],-20,-20,self.w+40,self.h+40)
        self.highlight:hide()
        
        self.model = self:add_actor('英雄2',10,10,self.w-20,self.h)
        self.tag = self:add_text('力量英雄',0,50,self.w-20,30,10,6)
        self.title = self:add_text('英雄2',0,self.h - 50,nil,30,16,5)

        return self
    end,

    set_data = function(self,temp)
        local ski1 = ac.lni('skill',temp['技能']) or empty
        self.skill = ski1
        self.model:set_actor(temp.name)
        self.tag:set_text(temp['主属性'])
        self.title:set_text(temp.title or temp.name)
    end,
    on_button_mouse_enter = function(self)
        if self.skill then
            local skill = self.skill
            local tip = format_obj(skill,skill.tip)
            self:tooltip(skill.title or skill.name,tip)
        end
    end
}


local ui = class.select_panel.create('玩家-UI选择英雄')
ui:set_level(99)
ui:set_select_button(3,class.herospSelect,300,400)
ui:hide()

--重置按钮
local refresh = ui:add_namebutton('刷新',(1920-300)/2,660,150,60,12,5)
ac.hero_refresh = refresh

function refresh:on_button_clicked()
    ac.sync('同步-刷新选择英雄')
end


--关闭
local esc = ui:add_namebutton('待会再选',(1920-300)/2 + 180,660,150,60,12,5)

function esc:on_button_clicked()
    ui:hide()
end

function ui:update()
    local player = ac.player.self
    local num = player:get('英雄刷新次数')
    local pay = player:get_data('付费英雄刷新',0)
    if num>0 then
        refresh.text:set_text(('免费刷新(|cff00ff00%0.f|r)'):format(num))
    else
        refresh.text:set_text(('刷新(|cffff0000%0.f|r杀敌)'):format(pay))
    end
end

ac.sync:event '玩家-UI选择英雄'(function(_,player,index)
    local info = player:get_data('选择英雄信息')
    local temp = info and info[1]
    local func = info and info[2]
    if temp and func then
        func(temp[index],index)
    end
end)


ac.game:event '玩家-选择英雄面板'(function(_,player,data,func)
    if #data==0 then
        log.error('选择英雄面板-选项为0')
        return
    end
    player:set_data('选择英雄信息',{data,func})
    if player:is_self() then
        ui:set_data(data)
        ui:show()
    end
end)

ac.game:event '玩家-显示选择英雄'(function(_,player)
    local info = player:get_data('选择英雄信息')
    if player:is_self() then
        ui:set_data(info[1])
        ui:show()
    end
end)
